/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events;


import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.AnonymousPawn;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.GenericPawn;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world.Chessboard;

/**
 * @author Massimo Canonaco
 *
 */
public class AttackEvent implements GenericEvent {

        /** */ 
        public static final float GLOBALLY_UNIQUE_IDENTIFIER = GUIDStorage.ATTACK_EVENT;
        
        /** */
        private String attackName;
        
        /**True means the attacked characters are alive and this isn't an event for their death. False means the characters has been attacked and they died.*/
        private boolean alive;
        
        private GenericPawn from;
        private GenericPawn to;
        private float totalDamage;
        
        /** */
        private boolean friendly;
        
        public AttackEvent(boolean success, boolean friendly, String attack, GenericPawn source, GenericPawn involved, final float damage)
        {
                attackName = attack;
                alive = success;
                from = source;
                to = involved;
                totalDamage = damage;
                this.friendly = friendly;
//              System.out.println("DANNO"+totalDamage+" "+alive);
        }

        @Override
        public float getGUID() 
        {
                return GLOBALLY_UNIQUE_IDENTIFIER;
        }

        @Override
        public String getName() 
        {
                return attackName;
        }

        /**
         * @return the successfull
         */
        public boolean isSuccessfull() {
                return alive;
        }

        /**
         * @return the from
         */
        public GenericPawn getFrom() {
                return from;
        }

        /**
         * @return the to
         */
        public GenericPawn getTo() {
                return to;
        }

        /**
         * @return the attackName
         */
        public String getAttackName() {
                return attackName;
        }

        /**
         * @return the totalDamage
         */
        public float getTotalDamage() {
                return totalDamage;
        }

        /**
         * @return the friendly
         */
        public boolean isFriendly() {
                return friendly;
        }


	/**
	 * @param friendly the friendly to set
	 */
	public void setFriendly(boolean friendly) {
		this.friendly = friendly;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject getAsJSONObject() 
	{
		JSONObject object = new JSONObject();
		
		object.put(GLOBAL_GUID_KEY, GLOBALLY_UNIQUE_IDENTIFIER);
		
		System.out.println("NOME ATTACCO: "+attackName);
		object.put(ATTACK_NAME_KEY, attackName);
		object.put(ALIVE_KEY, alive);
		object.put(FRIENDLY_KEY, friendly);
		object.put(DAMAGE_DEALTH_KEY, totalDamage);
		
		JSONArray arrayPosition = new JSONArray();
		arrayPosition.add(from.getRow());
		arrayPosition.add(from.getColumn());
		
		object.put(FROM_KEY, arrayPosition);
		
		arrayPosition = new JSONArray();
		arrayPosition.add(to.getRow());
		arrayPosition.add(to.getColumn());
		object.put(TO_KEY, arrayPosition);
		return object;
	}

	@Override
	public void setFromJSONOnbject(JSONObject object) 
	{
		System.out.println("imposto da "+object.toJSONString());
		attackName = (String) object.get(ATTACK_NAME_KEY);
		alive = (Boolean) object.get(ALIVE_KEY);
		friendly = (Boolean) object.get(FRIENDLY_KEY);
		totalDamage = new Float((Double) object.get(DAMAGE_DEALTH_KEY));
		
		JSONArray array = (JSONArray)object.get(FROM_KEY);
		from = new AnonymousPawn();
		from.setPosition(((Long)array.get(0)).intValue(), ((Long)array.get(1)).intValue());
		
		System.out.println("ORIGINE IN "+from.getRow()+" "+from.getColumn());

		array = (JSONArray)object.get(TO_KEY);
		to = new AnonymousPawn();
		to.setPosition(((Long)array.get(0)).intValue(), ((Long)array.get(1)).intValue());
	}
	
	static private final String ATTACK_NAME_KEY = "attackName";
	static private final String ALIVE_KEY = "aliveStatus";
	static private final String FRIENDLY_KEY = "friendlyStatus";
	static private final String FROM_KEY = "sourceCharacterPosition";
	static private final String TO_KEY = "targetCharacterPosition";
	static private final String DAMAGE_DEALTH_KEY = "damage";
}
